<?php

namespace App\Http\Controllers\Index;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Image;

class OrderController extends Controller
{
    // 订单中心
    public function udai_order(){
        $orders=DB::table('order')->where('uid',session('uid'))->orderBy('date','desc')->paginate(5);
        foreach($orders as $key=>$order){
            $goods[$key]=DB::table('order')->join('order_info','order_info.oid','=','order.id')->join('goodssize','goodssize.id','=','order_info.sid')->join('goods','goods.id','=','goodssize.gid')->where('order.id',$order->id)->select('goods.id','name','goods.img','price','color','size','num','order_info.state','order_info.id as ogid')->get();
        }
        if(!isset($goods)){
            $goods=[];
        }
    	return view('index/order/udai_order',['orders'=>$orders,'goods'=>$goods]);

        // $orders[0]=DB::table('order')->where('uid',session('uid'))->paginate(5);
        // for($i=1;$i<=4;$i++){
        //     $orders[$i]=DB::table('order')->join('order_info','order_info.oid','=','order.id')->where('uid',session('uid'))->where('state',$i-1)->paginate(5);
        // }
        // foreach($orders as $n=>$ordersn){
        //     foreach($ordersn as $key=>$order){
        //         $goods[$n][$key]=DB::table('order')->join('order_info','order_info.oid','=','order.id')->join('goodssize','goodssize.id','=','order_info.sid')->join('goods','goods.id','=','goodssize.gid')->where('order.id',$order->id)->select('goods.id','name','goods.img','price','color','size','num','order_info.state','order_info.id as ogid')->get();
        //     }
        // }

    }

    // 确认订单
    public function order_insert(Request $request){
        // dd($request->all());
        if($request->shopcartid){
            $goods=DB::table('shopcart')->whereIn('id',$request->shopcartid)->get();
        }else{
            $goods=DB::table('goods')->join('goodssize','goodssize.gid','=','goods.id')->where('goodssize.id',$request->sid)->select('goodssize.id as sid')->get();
            $goods[0]->num=$request->num;
        }
        // $info='';
        
        // $info=rtrim($info,'+');
        // 加入订单
        // 删除购物车商品
        if(DB::table('order')->insert(['o_id'=>$request->oid,'uid'=>$request->uid,'cost'=>$request->cost,'address'=>$request->addr,'date'=>$request->date])){
            if($request->shopcartid){
                DB::table('shopcart')->whereIn('id',$request->shopcartid)->delete();
            }
            $oid=DB::table('order')->where('o_id',$request->oid)->value('id');
            // 完善订单商品信息
            foreach($goods as $list){
                DB::table('order_info')->insert(['oid'=>$oid,'sid'=>$list->sid,'num'=>$list->num]);
                // $info.=$list->sid.'&'.$list->num.'+';
            }
            // $id=DB::table('order')->where('oid',$request->oid)->value('id');
            return redirect()->action('index\OrderController@order_pay',['id'=>$oid]);
        }
        // return view('index/order/order_insert');
    }

    // 订单结算
    public function order_pay(Request $request){
        $order=DB::table('order')->find($request->id);
        // $ginfo=DB::table('order')->where('id',$request->id)->value('info');
        // $infolist=explode('+',$ginfo);
        // // dd($infolist);
        // foreach($infolist as $ilist){
        //     $ii=explode('&',$ilist);
        //     $glist[$ii[0]]=$ii[1];
        // }
        // // dd($glist);
        $goods=DB::table('order')->join('order_info','order_info.oid','=','order.id')->join('goodssize','goodssize.id','=','order_info.sid')->join('goods','goods.id','=','goodssize.gid')->where('order.id',$request->id)->select('goods.id','name','goods.img','price','color','size','num')->get();
        // $goods=DB::table('goods')->join('goodssize','goods.id','=','goodssize.gid')->whereIn('goodssize.id',array_keys($glist))->select('goods.id','goodssize.id as sid','name','goods.img','price','color','size')->get();
        // foreach($goods as $gg){
        //     $gg->num=$glist[$gg->sid];
        // }
        // dd($goods);
        return view('index/order/udai_order_pay',['order'=>$order,'goods'=>$goods]);
    }

    // 支付成功
    public function order_paydone(Request $request){
        // dd($request->all());
        // 修改订单状态
        if(DB::table('order')->join('order_info','order_info.oid','=','order.id')->where('o_id',$request->o_id)->update(['order_info.state'=>'1'])){
            return redirect()->action('index\OrderController@udai_order');
        }
    }

    // 取消订单
    public function order_cancel(Request $request){
        if(DB::table('order_info')->where('oid',$request->id)->update(['state'=>'5'])){
            return redirect()->action('index\OrderController@udai_order');
        }
    }

    // 订单详情
    public function udai_order_detail(Request $request){
        $order=DB::table('order')->find($request->id);
        $goods=DB::table('order')->join('order_info','order_info.oid','=','order.id')->join('goodssize','goodssize.id','=','order_info.sid')->join('goods','goods.id','=','goodssize.gid')->where('order.id',$request->id)->select('goods.id','name','goods.img','price','color','size','num','order_info.state','order_info.id as ogid')->get();
    	return view('index/order/udai_order_detail',['order'=>$order,'goods'=>$goods]);
    }
    // 订单-确认收货
    public function udai_order_receipted(Request $request){
        $order=DB::table('order')->find($request->id);
        $goods=DB::table('order')->join('order_info','order_info.oid','=','order.id')->join('goodssize','goodssize.id','=','order_info.sid')->join('goods','goods.id','=','goodssize.gid')->where('order.id',$request->id)->select('goods.id','name','goods.img','price','color','size','num')->get();

    	return view('index/order/udai_order_receipted',['order'=>$order,'goods'=>$goods]);
    }
    // 确认收货成功
    public function order_receipteddone(Request $request){
        // dd($request->all());
        if(DB::table('order')->join('order_info','order_info.oid','=','order.id')->where('order.id',$request->oid)->update(['order_info.state'=>'3'])){
            return redirect()->action('index\OrderController@udai_order');
        }
    }
    // 订单评价
    public function udai_order_evaluate(Request $request){
        $goods=DB::table('order')->join('order_info','order_info.oid','=','order.id')->join('goodssize','goodssize.id','=','order_info.sid')->join('goods','goods.id','=','goodssize.gid')->where('order_info.id',$request->id)->select('goodssize.id','name','goods.img','price','color','size','date','order_info.id as ogid')->first();

        // $goods=DB::table('goods')->join('goodssize','goodssize.gid','=','goods.id')->where('goodssize.id',$request->id)->first();
        return view('index/order/udai_order_evaluate',['goods'=>$goods]);
    }

    // 提交评论
    public function evaluate_insert(Request $request){
        // dd($request->file('i'));
        $imgall='';
        foreach($request->file('i') as $rimg){
            $img=Image::make($rimg);
            $rdm=time().mt_rand(1000,9999);
            $imgname=$rdm.'.jpg';
            $img->save('upload/'.$imgname);
            $imgall.=$imgname.'&&';
        }
        $imgall=rtrim($imgall,'&&');
        // dd($imgall);
        $data=$request->except(['i','ogid']);
        $data['imgs']=$imgall;
        // 添加评论
        if(DB::table('goodscomment')->insert($data)){
            // 更新订单状态
            DB::table('order_info')->where('id',$request->ogid)->update(['state'=>'4']);
            // 商品评论数+1
            DB::table('goods')->join('goodssize','goodssize.gid','=','goods.id')->where('goodssize.id',$request->sid)->increment('goods.cmt_count');
            return redirect()->action('index\OrderController@udai_order');
        }
    }

    // 订单删除
    public function order_del(Request $request){
        if(DB::table('order')->where('id',$request->id)->delete() && DB::table('order_info')->where('oid',$request->id)->delete()){
            return redirect()->action('index\OrderController@udai_order');
        }
    }

    // 订单-退货
    public function udai_refund(){
    	return view('index/order/udai_refund');
    }
}
